﻿using MesErreurs;
using Metier;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Gestion_commerciale
{
    public partial class FCommande : Form
    {
        public FCommande()
        {
            InitializeComponent();
            
            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
            lvcdes.Items.Clear();
            lvcdes.View = View.Details;
            lvcdes.Columns.Add("Numéro Article", 100, HorizontalAlignment.Left);
            lvcdes.Columns.Add("Nom Article", 100, HorizontalAlignment.Left);
            lvcdes.Columns.Add("Prix unitaire", 100, HorizontalAlignment.Left);
            lvcdes.Columns.Add("Quantité commandée", 100, HorizontalAlignment.Left);

            initCbCommande();
            List<String> mesNumeros;
            try
            {
                // Lecture classique
                Article unArticle = new Article();
                mesNumeros = unArticle.LectureNoArticle();
                foreach (String item in mesNumeros)
                {
                    cb_Article.Items.Add(item);
                }
            }
            catch (MonException exception)
            {
                MessageBox.Show(exception.MessageApplication(), exception.Message);
            }
        }

        private void initCbCommande()
        {
            cb_Commande.Text = "";
            List<String> mesNumeros;
            try
            {
                // Lecture classique
                Commande uneCom = new Commande();
                mesNumeros = uneCom.LectureNoCommande();
                foreach (String item in mesNumeros)
                {
                    cb_Commande.Items.Add(item);
                }
            }
            catch (MonException exception)
            {
                MessageBox.Show(exception.MessageApplication(), exception.Message);
            }

            cb_Article.Enabled = false;
            bt_Plus.Enabled = false;
            bt_Moins.Enabled = false;
            bt_Supprimer.Enabled = false;
            bt_Modifier.Enabled = false;
        }

        private void AfficherListe()
        {
            List<Article> mesArticles;
            DetailCde unDetail = new DetailCde();
            string numCommande, numArt, nomArt;
            int quantite;
            float prix;
            ListViewItem lvitem_cde;

            numCommande = cb_Commande.Text;
            mesArticles = unDetail.getLesArticles(numCommande);

            lvcdes.Items.Clear();
            lvcdes.Columns.Clear();
            lvcdes.View = View.Details;
            lvcdes.Columns.Add("1", "Numéro Article");
            lvcdes.Columns.Add("2", "Nom Article");
            lvcdes.Columns.Add("3", "Prix Unitaire");
            lvcdes.Columns.Add("4", "Quantité");
            lvcdes.FullRowSelect = true;
            try
            {
                foreach (Article c in mesArticles)
                {
                    numArt = c.NoArticle;
                    unDetail.RechercheUnDetail(numCommande, numArt);
                    nomArt = c.LibArticle;
                    quantite = unDetail.QteCommandee;
                    prix = c.PrixArticle;
                    lvitem_cde = new ListViewItem(new string[] { numArt, nomArt, prix.ToString(), quantite.ToString() }, -1, Color.Black, Color.LightGreen, null);
                    lvcdes.Items.Add(lvitem_cde);
                }
                lvcdes.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
            }
            catch (MonException erreur)
            {
                throw erreur;
            }
        }

        private void bt_Interroger_Click(object sender, EventArgs e)
        {
            if (cb_Commande.Text == "") return;

            cb_Article.Enabled = true;
            bt_Plus.Enabled = true;
            bt_Moins.Enabled = true;
            bt_Supprimer.Enabled = true;
            bt_Modifier.Enabled = true;

            AfficherListe();
        }

        private void bt_Quantite_Click(object sender, EventArgs e)
        {
            if (cb_Article.Text != "")
            {
                DetailCde detail = new DetailCde();
                detail = detail.RechercheUnDetail(cb_Commande.Text, cb_Article.Text);
                if (sender == bt_Plus)
                {
                    if (detail.QteCommandee != 0)
                    {
                        detail.QteCommandee++;
                        detail.updateDetail();
                    }
                    else
                    {
                        detail = new DetailCde(cb_Commande.Text, cb_Article.Text, 1, "F");
                        detail.insererDetail();
                    }
                }
                else
                {
                    if (detail.QteCommandee > 1)
                    {
                        detail.QteCommandee--;
                        detail.updateDetail();
                    }
                    else if (detail.QteCommandee == 1)
                    {
                        detail.supprimerDetail();
                    }
                }
                AfficherListe();
            }
        }

        private void bt_Supprimer_Click(object sender, EventArgs e)
        {
            if (lvcdes.SelectedItems.Count > 0)
            {
                var confirmation = MessageBox.Show("Voulez vous vraiment supprimer la commande ?", "Suppression", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (confirmation == DialogResult.Yes)
                {
                    DetailCde detail = new DetailCde();
                    Commande commande = new Commande();

                    for (int i = 0; i < lvcdes.Items.Count; i++)
                    {
                        detail = detail.RechercheUnDetail(cb_Commande.Text, lvcdes.Items[i].SubItems[0].Text);
                        detail.supprimerDetail();
                    }
                    commande.RechercheUneCommande(cb_Commande.Text);
                    commande.supprimerCommande();
                    lvcdes.Items.Clear();
                    lvcdes.Columns.Clear();
                    initCbCommande();
                }
            }
            else MessageBox.Show("Sélectionner une commande");
        }

        private void bt_Ajouter_Click(object sender, EventArgs e)
        {
            FInser_Cde finser = new FInser_Cde();
            finser.ShowDialog();
            initCbCommande();
        }

        private void bt_Modifier_Click(object sender, EventArgs e)
        {
            FModif_Cde fModif = new FModif_Cde(cb_Commande.Text);
            fModif.ShowDialog();
        }
    }
}
